{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 多层感知机的简洁实现\n",
    "\n",
    "下面我们使用nn来实现上一节中的多层感知机。首先导入所需的包或模块。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import d2ltorch as d2lt\n",
    "import torch\n",
    "from torch import nn, optim"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 定义模型\n",
    "\n",
    "和softmax回归唯一的不同在于，我们多加了一个全连接层作为隐藏层。它的隐藏单元个数为256，并使用ReLU函数作为激活函数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "class MyMLP(nn.Module):\n",
    "    def __init__(self, **kwargs):\n",
    "        super(MyMLP, self).__init__(**kwargs)\n",
    "        self.mlp = nn.Sequential(\n",
    "            nn.Linear(28 * 28, 256),\n",
    "            nn.ReLU(),\n",
    "            nn.Linear(256, 10)\n",
    "        )\n",
    "    \n",
    "    def forward(self, x):\n",
    "        return self.mlp(x.reshape(-1, 28*28))\n",
    "\n",
    "net = MyMLP()\n",
    "d2lt.params_init(net, nn.init.normal_, std=0.01)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 读取数据并训练模型\n",
    "\n",
    "我们使用与[“softmax回归的简洁实现”](softmax-regression-gluon.ipynb)一节中训练softmax回归几乎相同的步骤来读取数据并训练模型。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "epoch 1, loss 0.0031, train acc 0.704, test acc 0.795\n",
      "epoch 2, loss 0.0019, train acc 0.823, test acc 0.834\n",
      "epoch 3, loss 0.0016, train acc 0.844, test acc 0.836\n",
      "epoch 4, loss 0.0015, train acc 0.855, test acc 0.744\n",
      "epoch 5, loss 0.0014, train acc 0.864, test acc 0.855\n"
     ]
    }
   ],
   "source": [
    "root = '~/dataset/'\n",
    "batch_size = 256\n",
    "train_iter, test_iter = d2lt.load_data_fashion_mnist(root, batch_size=batch_size)\n",
    "\n",
    "loss = nn.CrossEntropyLoss()\n",
    "optimizer = optim.SGD(net.parameters(), lr=0.5)\n",
    "num_epochs = 5\n",
    "d2lt.train_ch3(net, train_iter, test_iter, loss, num_epochs, batch_size, None, \n",
    "              None, optimizer)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 小结\n",
    "\n",
    "* 通过PyTorch可以更简洁地实现多层感知机。\n",
    "\n",
    "## 练习\n",
    "\n",
    "* 尝试多加入几个隐藏层，对比上一节中从零开始的实现。\n",
    "* 使用其他的激活函数，看看对结果的影响。\n",
    "\n",
    "\n",
    "\n",
    "## 扫码直达[讨论区](https://discuss.gluon.ai/t/topic/738)\n",
    "\n",
    "![](../img/qr_mlp-gluon.svg)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:pytorch]",
   "language": "python",
   "name": "conda-env-pytorch-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.9"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
